import { encode, decode } from "./hex-encode-decode";

// test short text encoding
function runEncodeShort() {
    let inputText = 'hello'
    let startTime = getDate()
    let encodes
    for (let i = 0; i < 5000; i++) {
        encodes = encode(inputText)
        // debugLog(`encode result: ${encodes}`);
    }
    let endTime = getDate()
    let expectedValue = '68656c6c6f'
    if (encodes !== expectedValue) {
        throw new Error('encode failed')
    }
    print(`hex-encode-decode_Encode_Func_0100: ${endTime - startTime} ms`)
}


// test long text encoding
function runEncodeLong() {
    let inputText = '<!@#$%^//Encode*~~>1234<!@#$%^//Encode*~~><!@#$%^//Encode*~~><!@#$%^//Encode*~~><!@#$%^//Encode*~~><!@#$%^//Encode*~~><!@#$%^//Encode*~~><!@#$%^//Encode*~~><!@#$%^//Encode*~~>'
    let startTime = getDate()
    let encodes
    for (let i = 0; i < 5000; i++) {
        encodes = encode(inputText)
        // debugLog(`encode result: ${encodes}`);
    }
    let endTime = getDate()
    let expectedValue = '3c21402324255e2f2f456e636f64652a7e7e3e313233343c21402324255e2f2f456e636f64652a7e7e3e3c21402324255e2f2f456e636f64652a7e7e3e3c21402324255e2f2f456e636f64652a7e7e3e3c21402324255e2f2f456e636f64652a7e7e3e3c21402324255e2f2f456e636f64652a7e7e3e3c21402324255e2f2f456e636f64652a7e7e3e3c21402324255e2f2f456e636f64652a7e7e3e3c21402324255e2f2f456e636f64652a7e7e3e'
    if (encodes !== expectedValue) {
        throw new Error('encode failed')
    }
    print(`hex-encode-decode_Encode_Func_0200: ${endTime - startTime} ms`)
}


// test short text decoding
function runDecodeShort() {
    let content = 'ABCDEFGHIJKabcdefj123456+/AA==!@';
    let encodes = encode(content);
    let startTime = getDate()
    let decodes
    for (let i = 0; i < 5000; i++) {
        decodes = decode(encodes)
        // debugLog(`decode result: ${decodes}`);
    }
    let endTime = getDate()
    if (content !== decodes) {
        throw new Error('decode failed')
    }
    print(`hex-encode-decode_Decode_Func_0100: ${endTime - startTime} ms`);
}


// test long text decoding
function runDecodeLong() {
    let content = '~!@#$%^&*()"<">///``223add~!@#$%^&*()"<">///~!@#$%^&*()"<">///~!@#$%^&*()"<">///~!@#$%^&*()"<">///~!@#$%^&*()"<">///~!@#$%^&*()"<">///~!@#$%^&*()(!&#%#&@^*#(@&!($&)!$*)$*)$*)$#&$^>><<&(*<>';
    let encodes = encode(content);
    let startTime = getDate()
    let decodes
    for (let i = 0; i < 5000; i++) {
        decodes = decode(encodes)
         // debugLog(`decode result: ${decodes}`);
    }
    let endTime = getDate()
    if (content !== decodes) {
        throw new Error('decode failed')
    }
    print(`hex-encode-decode_Decode_Func_0200: ${endTime - startTime} ms`);
}


function getDate() {
    return Date.now()
}

function debugLog(str) {
    // print(str)
}


runEncodeShort()
runEncodeLong()
runDecodeShort()
runDecodeLong()

